//
//  ViewController.swift
//  learnPrverserMargins
//
//  Created by nmxy_IOS on 2020/12/28.
//

import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.layoutMargins = UIEdgeInsets (top: 50 , left: 50, bottom: 50, right: 50)
        self.view.translatesAutoresizingMaskIntoConstraints = false

        /*
      指定情形 preservesSuperviewLayoutMargins 属性才会生效
       - 1、容器视图以 edge 进行约束.
       - 2、内容视图以 margins 进行边距约束.
       - 3、容器视图的的 margins 小于其父视图的 margins
      */
        
        let redView = UIView ()
        redView.translatesAutoresizingMaskIntoConstraints = false
        redView.preservesSuperviewLayoutMargins = true
        redView.backgroundColor = UIColor.red
        self.view.addSubview(redView)
        NSLayoutConstraint.activate([
            redView.leftAnchor.constraint(equalTo: self.view.leftAnchor),
            redView.topAnchor.constraint(equalTo: self.view.topAnchor),
            redView.rightAnchor.constraint(equalTo: self.view.rightAnchor),
            redView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
        ])

        
        let yellowView = UIView ()
        yellowView.translatesAutoresizingMaskIntoConstraints = false
        yellowView.backgroundColor = UIColor.yellow
        redView.addSubview(yellowView)
        let redMargins = redView.layoutMarginsGuide
        NSLayoutConstraint.activate([
            yellowView.leftAnchor.constraint(equalTo: redMargins.leftAnchor),
            yellowView.topAnchor.constraint(equalTo: redMargins.topAnchor),
            yellowView.rightAnchor.constraint(equalTo: redMargins.rightAnchor),
            yellowView.bottomAnchor.constraint(equalTo: redMargins.bottomAnchor)
        ])
 
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.present(UIStackViewViewController(), animated: true, completion: nil)
    }


}

